package ch.zbw.timeline.client.domain;

import java.rmi.Naming;
import java.rmi.RemoteException;

import org.apache.log4j.Logger;

import ch.zbw.timeline.client.ClientApp;
import ch.zbw.timeline.server.domain.Server;

public class Connection {

	private final static Logger LOGGER = Logger.getLogger(Connection.class);

	/**
	 * Connects to the server via RMI
	 * 
	 * @param serverName
	 *            name of the server
	 * @return Successful connected?
	 */
	public static Server connectToServer(String url) {
		try {
			Server server = (Server) Naming.lookup(url);
			return server;
		} catch (Exception e) {
			if (LOGGER.isDebugEnabled()) {
				LOGGER.debug("Fehler beim Verbindungsaufbau zum Server: " + e.getMessage());
			}
			return null;
		}
	}

	/**
	 * Fires a test method from the server
	 * 
	 * @param server
	 *            server
	 */
	private static void testConnection(Server server) {
		if (LOGGER.isDebugEnabled()) {
			try {
				LOGGER.debug(server.sayHello());
			} catch (RemoteException e) {
				LOGGER.debug("Test-Methode fehlgeschlagen");
			}
		}
	}
}
